package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclphbsjBaDetailCBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclphbsjBaDetailCVo;
import com.ruoyi.report_wjjhl.domain.WdclphbsjBaDetailC;
import com.ruoyi.report_wjjhl.mapper.WdclphbsjBaDetailCMapper;
import com.ruoyi.report_wjjhl.service.IWdclphbsjBaDetailCService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料配合比设计试验报告详细cService业务层处理
 *
 * @author zyb
 * @date 2024-08-21
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclphbsjBaDetailCServiceImpl implements IWdclphbsjBaDetailCService {

    private final WdclphbsjBaDetailCMapper baseMapper;

    /**
     * 查询无机结合料稳定材料配合比设计试验报告详细c
     */
    @Override
    public WdclphbsjBaDetailCVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询无机结合料稳定材料配合比设计试验报告详细c列表
     */
    @Override
    public TableDataInfo<WdclphbsjBaDetailCVo> queryPageList(WdclphbsjBaDetailCBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclphbsjBaDetailC> lqw = buildQueryWrapper(bo);
        Page<WdclphbsjBaDetailCVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料配合比设计试验报告详细c列表
     */
    @Override
    public List<WdclphbsjBaDetailCVo> queryList(WdclphbsjBaDetailCBo bo) {
        LambdaQueryWrapper<WdclphbsjBaDetailC> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclphbsjBaDetailC> buildQueryWrapper(WdclphbsjBaDetailCBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclphbsjBaDetailC> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, WdclphbsjBaDetailC::getExcelId, bo.getExcelId());
        lqw.eq(bo.getQualId() != null, WdclphbsjBaDetailC::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), WdclphbsjBaDetailC::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, WdclphbsjBaDetailC::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, WdclphbsjBaDetailC::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), WdclphbsjBaDetailC::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, WdclphbsjBaDetailC::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), WdclphbsjBaDetailC::getJiegpd, bo.getJiegpd());
        lqw.eq(bo.getYancsj() != null, WdclphbsjBaDetailC::getYancsj, bo.getYancsj());
        lqw.eq(bo.getShejz() != null, WdclphbsjBaDetailC::getShejz, bo.getShejz());
        lqw.eq(bo.getPingjz() != null, WdclphbsjBaDetailC::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, WdclphbsjBaDetailC::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getPiancxs() != null, WdclphbsjBaDetailC::getPiancxs, bo.getPiancxs());
        lqw.eq(StringUtils.isNotBlank(bo.getWucxkyjg()), WdclphbsjBaDetailC::getWucxkyjg, bo.getWucxkyjg());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料配合比设计试验报告详细c
     */
    @Override
    public Boolean insertByBo(WdclphbsjBaDetailCBo bo) {
        WdclphbsjBaDetailC add = BeanUtil.toBean(bo, WdclphbsjBaDetailC.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料配合比设计试验报告详细c
     */
    @Override
    public Boolean updateByBo(WdclphbsjBaDetailCBo bo) {
        WdclphbsjBaDetailC update = BeanUtil.toBean(bo, WdclphbsjBaDetailC.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclphbsjBaDetailC entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料配合比设计试验报告详细c
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
